package com.qiniu.githubdata.service;

import com.qiniu.githubdata.mapper.DeveloperNodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.kohsuke.github.GHRepository;

import com.qiniu.githubdata.entity.Developer;
import com.qiniu.githubdata.entity.neo.ContributionRelation;
import com.qiniu.githubdata.entity.neo.DeveloperNode;
// import com.qiniu.githubdata.mapper.DeveloperNodeRepository;


@Service
public class DeveloperSyncService {
    @Autowired
    private DeveloperService developerService;
    
    @Autowired
    private DeveloperNodeRepository developerNodeRepository;
    
    @Transactional
    public void syncDeveloperData(Developer developer, Map<String, GHRepository> repos) {
        // 1. Save to MySQL
        developerService.saveOrUpdate(developer);
        
        // 2. Create Neo4j node
        DeveloperNode developerNode = new DeveloperNode();
        developerNode.setId(developer.getId());
        developerNode.setLogin(developer.getLogin());
        developerNode.setNation(developer.getNation());
        developerNode.setNationConfidence(developer.getNationConfidence());
        
        // 3. Save contribution relationships
        Set<ContributionRelation> contributions = new HashSet<>();
//        for (Map.Entry<String, GHRepository> entry : repos.entrySet()) {
//            ContributionRelation relation = new ContributionRelation();
//            relation.setDeveloperNode(developerNode);
//            relation.setRepositoryName(entry.getKey());
//            contributions.add(relation);
//        }
//
//        developerNode.setContributions(contributions);
        developerNodeRepository.save(developerNode);
    }
}